package xuanmu.office.excel.data.generator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import xuanmu.office.excel.data.generator.entity.RowData;
import xuanmu.office.excel.data.generator.generator.RowDataGenerator;
import xuanmu.office.excel.data.generator.util.CsvUtils;
import xuanmu.office.excel.data.generator.util.FileUtils;

import java.io.File;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

@SpringBootApplication
public class ExcelDataGeneratorApplication {

    public static void main(String[] args) throws Exception {
        // 启动项目
        ConfigurableApplicationContext context = SpringApplication.run(ExcelDataGeneratorApplication.class, args);

        // 读取配置
        ConfigurableEnvironment config = context.getEnvironment();
        String exportDirectory = config.getProperty("custom.file.export-directory");
        System.out.println("【系统】读取配置，导出的文件将保存在【" + exportDirectory + "】，请确保此文件夹是存在的，并具有读写权限");

        // 提示输入年
        Scanner scanner = new Scanner(System.in);
        int defaultYear = LocalDate.now().getYear();
        System.out.println("【操作】请输入年（直接回车表示当前年：" + defaultYear + "）：");
        String yearString = scanner.nextLine();
        if (yearString.isEmpty()) {
            yearString = defaultYear + "";
        }
        int year = Integer.parseInt(yearString);
        // 提示输入月
        int defaultMonthValue = LocalDate.now().getMonthValue();
        System.out.println("【操作】请输入月（直接回车表示当前月" + defaultMonthValue + "）：");
        String monthString = scanner.nextLine();
        if (monthString.isEmpty()) {
            monthString = defaultMonthValue + "";
        }
        int month = Integer.parseInt(monthString);
        // 提示输入日
        int defaultDayOfMonth = LocalDate.now().getDayOfMonth();
        System.out.println("【操作】请输入日（直接回车表示当前日" + defaultDayOfMonth + "）：");
        String dayOfMonthString = scanner.nextLine();
        if (dayOfMonthString.isEmpty()) {
            dayOfMonthString = defaultDayOfMonth + "";
        }
        int dayOfMonth = Integer.parseInt(dayOfMonthString);

        // 检查日期有效性
        try {
            LocalDate.of(year, month, dayOfMonth);
        } catch (DateTimeParseException e) {
            System.out.println("【错误】输入的日期无效：" + year + "-" + month + "-" + dayOfMonth);
            System.out.println("【系统】程序将终止运行");
            System.exit(0);
        }

        // 提示输入总周转数
        System.out.println("【操作】请输入总周转数：");
        int totalTurnoverVolume = Integer.parseInt(scanner.nextLine());

        // 准备生成数据
        System.out.println("【系统】准备开始生成随机数据 ……");
        System.out.println();

        // 获取随机生成数据工具并生成数据
        RowDataGenerator rowDataGenerator = context.getBean(RowDataGenerator.class);
        List<RowData> rowDataList = rowDataGenerator.generateRowDataList(year, month, dayOfMonth, totalTurnoverVolume);

        // 排序并统计生成的数据
        System.out.println("【系统】数据已经生成 ……");
        Collections.sort(rowDataList); // 排序，规则参考RowData的compareTo方法
        int currentTurnoverVolume = 0;
        for (RowData rowData : rowDataList) {
            currentTurnoverVolume += rowData.getTurnoverVolume();
        }
        System.out.println("【系统】以上数据的周转量为：" + currentTurnoverVolume);

        // 将数据写入到文件
        System.out.println("【系统】即将写入文件 ……");
        File exportCsvFile = FileUtils.getExportCsvFile(exportDirectory);
        PrintWriter writer = new PrintWriter(exportCsvFile);
        for (RowData rowData : rowDataList) {
            writer.println(CsvUtils.toCsvLine(rowData));
        }
        writer.flush();
        writer.close();

        // 完成
        System.out.println("【系统】写入完成，导出的文件：" + exportCsvFile);
    }

}
